package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: qingtao
 * @Date 2022/3/14 14:32
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    @Autowired(required = false)
    private HttpSession session;

    private Long getId(){
        return (Long) session.getAttribute("employee");
    }

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> findByPage(Integer page,Integer pageSize,String name){
        return R.success(setmealService.findByPage(page,pageSize,name));
    }

    /**
     * 添加
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        boolean save = setmealService.save(setmealDto,getId());
        return R.success("添加成功！");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        setmealService.delete(ids,getId());
        return R.success("删除成功");
    }

    /**
     * 修改状态
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam("ids") List<Long> ids,@PathVariable Integer status){
        setmealService.updateStatus(ids,status,getId());
        return R.success("修改成功");
    }

    /**
     * 修改
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.update(setmealDto,getId());
        return R.success("修改成功");
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    /**
     * 根据套餐的类别展示套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId, Integer status){
        List<Setmeal> setmealList = setmealService.list(categoryId,status );
        return R.success(setmealList);
    }

    @GetMapping("/dish")
    public R<List<SetmealDish>> findSetmealDish(Long setmealId){
        return R.success(setmealService.findSetmealDish(setmealId));
    }
}
